// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   UpdateJspBuilder.java

package tool.radmodel;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.apache.commons.io.FileUtils;

// Referenced classes of package com.qdone.radmodel:
//			CodeBuilder, Project, Entity, Field

public class UpdateJspBuilder
	implements CodeBuilder
{

	private Project project;
	private Entity entity;

	public UpdateJspBuilder(Project project)
	{
		this.project = project;
		entity = this.project.getEntity();
	}

	private void saveToFile(StringBuffer codeBuffer)
	{
		String dir = project.getActionPath();
		File fileDir = new File(dir);
		if (!fileDir.exists())
			try
			{
				FileUtils.forceMkdir(fileDir);
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		String fileName = (new StringBuilder(String.valueOf(project.getOutputPath()))).append("jsp").append(File.separator).append(entity.getTableName().toLowerCase()).append(File.separator).append("update.html").toString();
		File file = new File(fileName);
		if (file.exists())
			file.delete();
		try
		{
			FileUtils.writeStringToFile(file, codeBuffer.toString(), "utf-8");
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public void saveToFile()
	{
		StringBuffer actionBuffer = new StringBuffer();
		String file = (new StringBuilder(String.valueOf(project.getTemplatePath()))).append("updateHeader.txt").toString();
		String actionString;
		try
		{
			actionString = FileUtils.readFileToString(new File(file));
		}
		catch (IOException e)
		{
			actionString = "";
		}
		actionString = actionString.replaceAll("_prefix_", project.getPrefix());
		actionString = actionString.replaceAll("_EntityBeanName_", entity.getEntityBeanName());
		/*���datagridѭ�����*/
		StringBuffer dataArea=new StringBuffer();
		String primaryKey=entity.getPrimaryKeyField().getPropertyName();
		 //�������
		dataArea.append("            <td >"+primaryKey+":</td><td ><input id='"+primaryKey+"' name='"+primaryKey+"' class='mini-textbox'   /></td>\r\n");
		//�������������ֶ�
		for (Iterator<?> iterator = entity.getFields().iterator(); iterator.hasNext();)
		{
			
			Field field = (Field)iterator.next();
			dataArea.append("            <td >"+field.getPropertyName()+":</td><td ><input id='"+field.getPropertyName()+"' name='"+field.getPropertyName()+"' class='mini-textbox'   /></td>\r\n");
		}
		actionString = actionString.replaceAll("_DataGridCloumn_", dataArea.toString());
		actionBuffer.append(actionString);
		/*actionBuffer.append("\r\n");
		for (Iterator iterator = entity.getFields().iterator(); iterator.hasNext(); actionBuffer.append("\t\t</tr>\r\n"))
		{
			Field field = (Field)iterator.next();
			actionBuffer.append("\t\t<tr>\r\n");
			actionBuffer.append((new StringBuilder("\t\t\t<th><span class=\"required\">*</span>")).append(field.getFieldName()).append("</th>\r\n").toString());
			actionBuffer.append("\t\t\t<td>\r\n");
			actionBuffer.append((new StringBuilder("\t\t\t\t<input type=\"text\" id=\"")).append(field.getPropertyName()).append("\" title=\"\" class=\"input\"\r\n").toString());
			actionBuffer.append((new StringBuilder("\t\t\t\t\tname=\"")).append(entity.getEntityBeanName()).append(".").append(field.getPropertyName()).append("\" tip=\"\"").toString());
			actionBuffer.append((new StringBuilder(" value=\"${")).append(entity.getEntityBeanName()).append(".").append(field.getPropertyName()).append("}\" rules=\"required,trimBlank\"/>\r\n").toString());
			actionBuffer.append("\t\t\t</td>\r\n");
		}

		actionBuffer.append("\t</table>\r\n");
		actionBuffer.append("</form>\r\n");
		actionBuffer.append("</body>\r\n");
		actionBuffer.append("</html>\r\n");*/
		saveToFile(actionBuffer);
	}
}
